package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomEventHandler;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import org.flowable.engine.common.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.common.api.delegate.event.FlowableEvent;
import org.flowable.engine.common.api.delegate.event.FlowableEventType;
import org.flowable.engine.delegate.event.impl.FlowableProcessEventImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lilh
 * @date 2021/6/8 15:23
 */
@Component
public class ProcessInstanceCancelEventHandler implements CustomEventHandler {

    @Autowired
    private FlowProcessInstanceTraceRepository processInstanceTraceService;

    @Autowired
    private FlowTaskTraceRepository taskTraceRepository;


    @Override
    public FlowableEventType getEventType() {
        return FlowableEngineEventType.PROCESS_CANCELLED;
    }

    @Override
    public void handle(FlowableEvent event) {
        if (!(event instanceof FlowableProcessEventImpl)) {
            return;
        }
        String processInstanceId = ((FlowableProcessEventImpl)event).getProcessInstanceId();

        //将实例状态更新为结束
        processInstanceTraceService.cancelProcessInstance(processInstanceId);
    }
}
